﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SdmxMl.Manager;
using SdmxMl.Multiplexor;
using SdmxMl.Structure;

namespace SdmxMl.Module
{
    public partial class DlgModuleSelectorNew : Form
    {
        private DataSet ds;
        private ImageList imageListBook;

        #region Event

        /// <summary> Publishes the method definition in the form of a delegate </summary>
        /// <param name="sender"></param>/ <param name="e">CancelEventArgs instance</param>
        public delegate void ModuleSelectedEvent(object sender, ModuleBookEventArgs e);

        public event ModuleSelectedEvent ModuleSelected;
        public void RaiseModuleSelected(string fullId, TableOfContents moduleType)
        {
            ModuleBookEventArgs evArg = new ModuleBookEventArgs(fullId, moduleType);
            ModuleSelectedEvent eventHandler = this.ModuleSelected;

            if (eventHandler != null)
                eventHandler(this, evArg);
        }
        #endregion


        public string ModuleFullId { get; private set; }

        public DlgModuleSelectorNew(TableOfContents moduleType, List<TableOfContents> choices, string AgencyId, ImageList imgList)
        {
            InitializeComponent();
            imageListBook = imgList;

            //Make combo box draw mode to variable draw mode 
            //this will allow us to draw combo box in our style
            comboBoxArtefactType.DrawMode = DrawMode.OwnerDrawVariable;
            //using DrawItem event we need to draw item
            comboBoxArtefactType.DrawItem += new DrawItemEventHandler(comboBoxArtefactType_DraItemEvent);

            // Fill all module type availables
            foreach (TableOfContents toc in choices)
            {
                comboBoxArtefactType.Items.Add(new ComboArtefactTypeModuItem(toc));
                if (toc == moduleType)
                    comboBoxArtefactType.SelectedItem = comboBoxArtefactType.Items[comboBoxArtefactType.Items.Count - 1];
            }

            // Owner list
            

            // Agency List
        }

        private void comboBoxArtefactType_DraItemEvent(object sender, DrawItemEventArgs e)
        {
            ComboArtefactTypeModuItem comboItem = comboBoxArtefactType.Items[e.Index] as ComboArtefactTypeModuItem;
            //draw back groud of the item
            e.DrawBackground();
            //Create a Image from a file
            Image img = imageListBook.Images[(int)comboItem.ModuType];
            //Draw the image in combo box using its bound, here size of image is
            // 10, 10 you can increase the size if you want
            e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 15, 15);
            //we need to draw the item as string because we made drawmode to ownervariable
            e.Graphics.DrawString(comboItem.ToString(), comboBoxArtefactType.Font,
                System.Drawing.Brushes.Black,
                new RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
            //draw rectangle over the item selected
            e.DrawFocusRectangle();
        }

        private void buttonList_Click(object sender, EventArgs e)
        {
            ModuleFilter mf = new ModuleFilter(Mux.Instance.GetNewRegistry());
            ComboArtefactTypeModuItem comboItem = comboBoxArtefactType.Items[comboBoxArtefactType.SelectedIndex] as ComboArtefactTypeModuItem;
            string type = SoapRegProxy.ConvertOldTypeToNewType(comboItem.ArType.ToString());


            DataTable dt = mf.ApplyFilter(type, comboBoxOwner.Text, comboBoxAgency.Text, textBoxArtefactId.Text, textBoxVersion.Text);
            dataGridViewList.DataSource = dt;
        }
    }

    /// <summary>
    /// Class to correct some display in combo of artefact types
    /// </summary>
    internal class ComboArtefactTypeModuItem
    {
        internal SdmxArtefactType ArType { get; private set; }
        public TableOfContents ModuType { get; private set; }

        internal ComboArtefactTypeModuItem(TableOfContents moduleType)
        {
            ModuType = moduleType;
            ArType = moduleType.SdmxType();
        }
        public override string ToString()
        {
            return ModuType.ToString();
        }
    }
}
